<?php
ini_set('memory_limit', '-1');
ini_set('max_execution_time', 86400);
               
// Set flag that this is a parent file.
define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);

if (file_exists(dirname(__FILE__) . '/defines.php')) {
    include_once dirname(__FILE__) . '/defines.php';
}

if (!defined('_JDEFINES')) {
    define('JPATH_BASE', dirname(__FILE__));
    require_once JPATH_BASE.'/includes/defines.php';
}

require_once JPATH_BASE.'/includes/framework.php';

// Instantiate the application.
$app = JFactory::getApplication('site');

// Initialise the application.
$app->initialise();

require_once JPATH_BASE.DS.'administrator'.DS.'components'.DS.'com_wrd'.DS.'classes'.DS.'utilities'.DS.'reportProvider.php';
require_once JPATH_BASE.DS.'administrator'.DS.'components'.DS.'com_wrd'.DS.'classes'.DS.'utilities'.DS.'pointProvider.php';
require_once JPATH_BASE.DS.'administrator'.DS.'components'.DS.'com_wrd'.DS.'classes'.DS.'PHPExcel.php';

$db = JFactory::getDbo();
$conditions = array();

$conditions['dateFrom'] = strval($_GET['f']);
$conditions['dateTo'] = strval($_GET['t']);
$limit = strval($_GET['limit']);
{

// Init excel
$objPHPExcel = new PHPExcel();
// Create a first sheet, representing sales data
$objPHPExcel->setActiveSheetIndex(0);
$activeSheet = $objPHPExcel->getActiveSheet();
$activeSheet->mergeCells('A1:I1');
$activeSheet->setCellValue('A1', 'Master List Report');
$activeSheet->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$activeSheet->getStyle('A1')->getFont()->setSize(24);
$activeSheet->getStyle('A1')->getFont()->setBold(true);

if($limit != -1){

	$activeSheet->setCellValue('A2', 'Member Joining, Date From');
	$activeSheet->setCellValue('B2', strval($_GET['f']));

	$activeSheet->setCellValue('C2', 'Member Joining, Date To');
	$activeSheet->setCellValue('D2', strval($_GET['t']));
}
else if($limit == -1){
	 
	 $activeSheet->mergeCells('A2:C2');
	 $activeSheet->getStyle('A2'.$index)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
	 $activeSheet->setCellValue('A2', "Download All Members");
}


/************** FILL HEADER **************/
$index = 5;
$activeSheet->setCellValue('A'.$index, 'Member No');
$activeSheet->setCellValue('B'.$index, 'Title');
$activeSheet->setCellValue('C'.$index, 'Name');
$activeSheet->setCellValue('D'.$index, 'Date of Birth');
$activeSheet->setCellValue('E'.$index, 'Mobile No');
$activeSheet->setCellValue('F'.$index, 'Home No');
$activeSheet->setCellValue('G'.$index, 'Contact Email');
$activeSheet->setCellValue('H'.$index, 'Personnal Email');
$activeSheet->setCellValue('I'.$index, 'Address');
$activeSheet->setCellValue('J'.$index, 'District');
$activeSheet->setCellValue('K'.$index, 'City');
$activeSheet->setCellValue('L'.$index, 'Organisation Name');
$activeSheet->setCellValue('M'.$index, 'Occupation');
$activeSheet->setCellValue('N'.$index, 'Organisation Postal Address 1');
$activeSheet->setCellValue('O'.$index, 'Organisation Postal Address 3');
$activeSheet->setCellValue('P'.$index, 'Nationality');
$activeSheet->setCellValue('Q'.$index, 'Loyalty Points ');
$activeSheet->setCellValue('R'.$index, 'Member Joining Date');
$activeSheet->setCellValue('S'.$index, 'Application received date');
$activeSheet->setCellValue('T'.$index, 'Delivered VIPCard date');
$activeSheet->setCellValue('U'.$index, 'Application Received from');
$activeSheet->setCellValue('V'.$index, 'Card Pick up arrangement');
$activeSheet->setCellValue('W'.$index, 'Reapply Date');
$fill = $activeSheet->getStyle('A5:W5')->getFill();
$fill->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$fill->getStartColor()->setARGB('cc99ff');
$style = $activeSheet->getStyle('A3')->getFont();
$style->setSize(12);
$style->setBold(true);

$cols = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W');

foreach($cols as $item)
{
    if($item == 'C')
    {
        setBackgroundColor($activeSheet, $item, 5, 35);
    }
    else if($item == 'G' || $item == 'M')
    {
        setBackgroundColor($activeSheet, $item, 5, 40);
    }
	else if($item == 'B')
    {
        setBackgroundColor($activeSheet, $item, 5, 10);
    }
    else if($item == 'H' || $item == 'D')
    {
        setBackgroundColor($activeSheet, $item, 5, 28);
    }
    else
    {
        setBackgroundColor($activeSheet, $item, 5, 20);   
    }
}

}// End init excel

/******** Query Data ********/  

$data = reportProvider::memberreport($conditions,$db,$limit);

//echo "<pre>";print_r($data);exit;
if(!empty($data))
{
    $activeSheet->mergeCells('A3:H3');
    $activeSheet->setCellValue('A3', 'Total: '.$data['total'].'. Display top: '.count($data['items']));   
	$style = $activeSheet->getStyle('A3')->getFont();
    $style->setSize(16);
    $style->setBold(true);
    $style->setColor( new PHPExcel_Style_Color( 'CC0000' ) );
    
    /*********** BIND TO EXCEL ***********/
    $index = 6;
    $data = $data['items'];
    $size = count($data);
    for($i=0; $i<$size; $i++)
    {
		$activeSheet->setCellValue('A'.$index, $data[$i]->member_id);
		$activeSheet->getStyle('A'.$index)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		
		$activeSheet->setCellValue('B'.$index, $data[$i]->prefix);
		//$activeSheet->setCellValue('C'.$index, $data[$i]->prefix);
		
		
        $activeSheet->setCellValue('C'.$index, $data[$i]->l_name.' '.$data[$i]->f_name);
        //$activeSheet->getStyle('C'.$index)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $activeSheet->setCellValue('D'.$index,(strtotime($data[$i]->dob)?date("d-m-Y",strtotime($data[$i]->dob)):""));                
        $activeSheet->setCellValue('E'.$index, $data[$i]->phone);   
        $activeSheet->setCellValueExplicit('E'.$index, $data[$i]->phone, PHPExcel_Cell_DataType::TYPE_STRING);
		//$activeSheet->getStyle('E'.$index)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
                 
        $activeSheet->setCellValue('F'.$index, $data[$i]->referer_phone);            
        $activeSheet->setCellValueExplicit('F'.$index, $data[$i]->referer_phone, PHPExcel_Cell_DataType::TYPE_STRING);
		
		$activeSheet->setCellValue('G'.$index, $data[$i]->email);
		$activeSheet->setCellValue('H'.$index, $data[$i]->alternative_email);
		
        $activeSheet->setCellValue('I'.$index, $data[$i]->address);
		$activeSheet->setCellValue('J'.$index, $data[$i]->distric);
		$activeSheet->setCellValue('K'.$index, $data[$i]->city);
		$activeSheet->setCellValue('L'.$index, $data[$i]->company_name);
		$activeSheet->setCellValue('M'.$index, $data[$i]->o_job);
		$activeSheet->setCellValue('N'.$index, $data[$i]->c_addr);	
        $activeSheet->setCellValue('O'.$index, $data[$i]->c_distric);
		$activeSheet->setCellValue('P'.$index, $data[$i]->nationality);
		$activeSheet->setCellValue('Q'.$index, $data[$i]->p_o_u);
		
		$objPHPExcel->getActiveSheet()->getStyle('Q'.$index)->getNumberFormat()->setFormatCode("#,##0");
		
		$activeSheet->setCellValue('R'.$index, (strtotime($data[$i]->created_date)?date("d-m-Y",strtotime($data[$i]->created_date)):""));
		$activeSheet->setCellValue('S'.$index, (strtotime($data[$i]->non_pick_up_date)?date("d-m-Y",strtotime($data[$i]->non_pick_up_date)):"") );
		$activeSheet->setCellValue('T'.$index, (strtotime($data[$i]->pick_up_date)?date("d-m-Y",strtotime($data[$i]->pick_up_date)):"") );
		$activeSheet->setCellValue('U'.$index, $data[$i]->non_pick_up_at );
		$activeSheet->setCellValue('V'.$index, $data[$i]->pick_up_at);
		$activeSheet->setCellValue('W'.$index, (strtotime($data[$i]->reapply_date)?date("d-m-Y",strtotime($data[$i]->reapply_date)):""));
		$index++;
    }
    unset($data);
}
//exit;
$fileName = 'master_list_report_'.date('m_d_Y_H_i_s');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(JPATH_BASE.DS.'tmp'.DS.$fileName.'.xlsx');

$fullPath = JPATH_ROOT.DS.'tmp'.DS.$fileName.'.xlsx';
$fsize = filesize($fullPath);

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$fileName.'.xlsx"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $fsize);
ob_clean();

flush();
readfile( $fullPath );
exit;   
       
function setBackgroundColor(&$activeSheet, $colName, $colIndex, $width=15)
{
    $activeSheet->getColumnDimension($colName)->setWidth($width);    
    $style = $activeSheet->getStyle($colName.$colIndex);
    $style->getFont()->setSize(12);
    $style->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
}
